Musiker.modHardPlaylists = {
	name			: 	"HardPlaylists",
	currentPlaylist	: 	null,
	lockedPlaylist	: 	null,
	playlistContents: 	{},
	playlistsByName	: 	{}
}

Musiker.modHardPlaylists.Initialize = function () {
	var aFrame = document.createElement('div');
		aFrame.className = 'cssPlaylistContainerFrame';
		aFrame.show = MUSIKER_SHOWPANEL;
		aFrame.hide = MUSIKER_HIDEPANEL;
		aFrame.hide();
		Musiker.modHardPlaylists.Panel = aFrame
	Musiker.frames.DisplayPanel.appendChild(aFrame);
	
	//wrapping div
	var cFrame = document.createElement('div');
		cFrame.className = 'scrollingWrapper';
		cFrame.style.top = '17px';
		cFrame.style.bottom = '33px';
	Musiker.modHardPlaylists.Panel.appendChild(cFrame);	

	//contentTable
	var aFrame = document.createElement('table');
		aFrame.className = 'cssPlaylist';
		Musiker.modHardPlaylists.listPanel = aFrame;
	cFrame.appendChild(aFrame);
	
		var aFrame = document.createElement('div');
			aFrame.className = 'cssFillerFrame';
		Musiker.modHardPlaylists.listPanel.appendChild(aFrame);
		

	//header
	var aFrame = document.createElement('table');
		aFrame.className = 'cssPlaylistHeader';
		aFrame.innerHTML = "<tr class='cssPlaylistItem' header='yes'><td class='cssPlaylistItem_Num'>#</td><td class='cssPlaylistItem_Title'>Title</td><td class='cssPlaylistItem_Artist'>Artist</td><td class='cssPlaylistItem_Album'>Album</td><td class='cssPlaylistItem_Time'>Time</td><td class='cssPlaylistItem_Rating'>Rating</td><td></td></tr>";
	Musiker.modHardPlaylists.Panel.appendChild(aFrame);


	//action bar
	var aFrame = document.createElement('div');
		aFrame.className = 'cssActionBar';
		Musiker.modHardPlaylists.actionBarPanel = aFrame;
	Musiker.modHardPlaylists.Panel.appendChild(aFrame);

		var aFrame = document.createElement('div');
			aFrame.className = 'cssActionBarProgressFrame';
			aFrame.show = MUSIKER_SHOWPANEL;
			aFrame.hide = MUSIKER_HIDEPANEL;
			aFrame.set = function (percent) {
				this.fill.style.width = Math.round(percent)+'%';
			}
			Musiker.modHardPlaylists.actionBarPanel.progressBar = aFrame;
		Musiker.modHardPlaylists.actionBarPanel.appendChild(aFrame);
		
			var bFrame = document.createElement('div');
				bFrame.className = 'cssActionBarProgressFill';
				Musiker.modHardPlaylists.actionBarPanel.progressBar.fill = bFrame;
			aFrame.appendChild(bFrame);

/* Not yet implimented
		var aFrame = document.createElement('a');
			aFrame.className = 'cssActionBarButton';
			aFrame.innerHTML = 'Build M3U File';
			aFrame.setAttribute('enabled',true);
			aFrame.onclick = function () {}
			Musiker.modHardPlaylists.actionBarPanel.buttonBuildM3U = aFrame;
		Musiker.modHardPlaylists.actionBarPanel.appendChild(aFrame);  //disabled until I write the M3U script
*/

	//	Setup Sidebar Row --------------------------------------------------------------------------------------
	var aFrame = document.createElement('div');
		aFrame.className = 'cssSidebar_HeaderRow';
		aFrame.innerHTML = "Premade Playlists";
	Musiker.frames.SidebarPanel.appendChild(aFrame);
	for (pName in Musiker.modHardPlaylists.playlistsByName) {
		var aFrame = document.createElement('div');
			aFrame.className = 'cssSidebar_Row';
			aFrame.fileCode = Musiker.modHardPlaylists.playlistsByName[pName];
			if (Musiker.demoMode && Musiker.demoMode.AllowedPlaylists.indexOf(pName) == -1 ) {
				aFrame.innerHTML = "<img src='images/sidebarLockIcon.png' style='float:right'><img src='images/iTunesSmartPlaylist.png'><a>"+pName+'</a>';
			} else {
				aFrame.innerHTML = "<img src='images/iTunesSmartPlaylist.png'><a>"+pName+'</a>';
				aFrame.onclick = function () {
					Musiker.selectModule(Musiker.modHardPlaylists);		
					
					Musiker.Sidebar.setSelection(this);
					
					Musiker.modHardPlaylists.load(this.fileCode);
				}
			}
		Musiker.frames.SidebarPanel.appendChild(aFrame);
	}
}

Musiker.modHardPlaylists.Start = function () {
	
}

Musiker.modHardPlaylists.show = function () {
	Musiker.modHardPlaylists.Panel.show();
	
}
Musiker.modHardPlaylists.hide = function () {
	Musiker.modHardPlaylists.Panel.hide();
}


Musiker.modHardPlaylists.load = function (pathString, calledFromData) {
	//this function is called from both the userinterface, and by the loadPlaylistData.php
	//results.  If called from loadPlaylistData, calledFromData will be true.

	//if is a userinterface call, set the current displaying playlist
	if (!calledFromData) Musiker.modHardPlaylists.currentPlaylist = pathString;
	//if it is a call from the return, stop the function now if this isn't the current playlist
	else if (Musiker.modHardPlaylists.currentPlaylist != pathString) return;
	//this prevents slow results returns from overwriting if the user switches playlists during a load

	//clear the playlist frame of all contents
	while (Musiker.modHardPlaylists.listPanel.childNodes.length > 0) {
		Musiker.modHardPlaylists.listPanel.removeChild( Musiker.modHardPlaylists.listPanel.firstChild );       
	}
	Musiker.modHardPlaylists.listPanel.parentNode.scrollTop = 0;
	
	if (Musiker.modHardPlaylists.playlistContents[pathString]) {  //playlist content is loaded, building the frame	
		//drawing the playlist is handled by a seperate function that can be called via a timeout.
		//If the playlist is larger than 100 rows, the first 100 will be drawn immediately, and the
		//rest will be drawn in a timeout thread to prevent the interface from locking up.	
		Musiker.modHardPlaylists.actionBarPanel.progressBar.show();
		Musiker.modHardPlaylists.actionBarPanel.progressBar.set(0);
		Musiker.modHardPlaylists._bufferedPlaylistDrawing(pathString, 0, 100)
		
	} else {	//no playlist content loaded, fetch it
		var aFrame = document.createElement('div');
			aFrame.className = 'cssWaitFrame';
		Musiker.modHardPlaylists.listPanel.appendChild(aFrame);
		
		SimpleFetch.FetchAndEval('FetchHardPlaylistContent.js.php?path='+pathString);		
	}
}

Musiker.modHardPlaylists._bufferedPlaylistDrawing = function (pathString, startValue, stopCount) {
	//abort if the user has switched playlists
	if (Musiker.modHardPlaylists.currentPlaylist != pathString) return;
	
	var playlistData = Musiker.modHardPlaylists.playlistContents[pathString];

	//row items
	var oddfield = 0;
	var c = Math.min(startValue+stopCount,playlistData.length); //only draw up to what the function is allowed
	for (var i=startValue;i<c;i++) {
		var o = playlistData[i];
			o.title = o.name;
		oddfield = ++oddfield % 2;
	
		var aFrame = Musiker.buildRow(o,i+1,oddfield);
			aFrame.playIndex = i;
			aFrame.ondblclick = function () {
				Musiker.modHardPlaylists.playlistLockAndLoad();
				MediaPlayer.playTrack(this.playIndex);
				return false;
			}
		Musiker.modHardPlaylists.listPanel.appendChild(aFrame);
	}
	
	//if there's still playlist data remaining, queue up next batch
	if (i<playlistData.length-1) {
		Musiker.modHardPlaylists.actionBarPanel.progressBar.set((i/playlistData.length)*100);
		setTimeout('Musiker.modHardPlaylists._bufferedPlaylistDrawing("'+pathString+'", '+i+',50)',1);
	} else Musiker.modHardPlaylists.actionBarPanel.progressBar.hide();
}

Musiker.modHardPlaylists.playlistLockAndLoad = function () {
	if (Musiker.modHardPlaylists.lockedPlaylist == Musiker.modHardPlaylists.currentPlaylist) return; //loaded into player
	
	Musiker.modHardPlaylists.lockedPlaylist = Musiker.modHardPlaylists.currentPlaylist;
	
	var deleteFirst = true;
	if (MediaPlayer.defaultState()) deleteFirst = false;
	else MediaPlayer.removeAllTracks();
	
	if (MediaPlayer.currentItemIndex==0) Musiker.modPlayer.clearStatus();
	
	var playlistData = Musiker.modHardPlaylists.playlistContents[Musiker.modHardPlaylists.currentPlaylist];
	var c = playlistData.length-1;
	for (var i=0;i<c;i++) {
		var o = playlistData[i];
				
		MediaPlayer.appendToPlaylist(
			o.realpath.replace('\+','\%2B'), //the replacement is because mediaplayer thinks + is a space and requests the wrong file
			o.name,
			o.artist,
			o.encodedpath,
			o.album
		);
	}
	
	if (deleteFirst) MediaPlayer.removeTrack(0); //removeAllTracks always leaves the currently loaded track behind, cant be removed until more tracks are loaded
}

var MUSIKER_STARS = Array(
	"<img src='images/itunesStars0.gif'>",
	"<img src='images/itunesStars1.gif'>",
	"<img src='images/itunesStars2.gif'>",
	"<img src='images/itunesStars3.gif'>",
	"<img src='images/itunesStars4.gif'>",
	"<img src='images/itunesStars5.gif'>"
);

//insert the script tag to load the playlists list
var aFrame = document.createElement('script');
	aFrame.setAttribute('src', "FetchHardPlaylistList.js.php");
	document.body.appendChild(aFrame);

Musiker.RegisterModule(Musiker.modHardPlaylists);